﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace working
{
    public partial class DetailForm : Form
    {
        public Action<WorkItem> onApply { get; set; }
        public Action onHide { get; set; }
        private WorkItem workItem;
        private bool changed;
        private CmdAgent agent;
        private List<Process> opendFolder = new List<Process>();
        public DetailForm()
        {
            InitializeComponent();
            agent = new CmdAgent();
            listView1.DoubleClick += OnDoubleSelectLinkItem;
            listView1.Click += OnSelectLinkItem;
        }

        public void Show(WorkItem workItem)
        {
            this.workItem = workItem;
            if (workItem != null)
            {
                changed = false;
                ReLoadWorkItemInfo();
                this.Show();
            }
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            base.OnClosing(e);
            if (changed && this.workItem != null)
            {
                changed = false;
                onApply(workItem);
            }
            close_opend_folders();
            Hide();
            if(onHide != null) {
                onHide.Invoke();
            }
        }

        private void ReLoadWorkItemInfo()
        {
            title.Text = workItem.id + " " + workItem.name;
            this.listView1.Items.Clear();
            int i = 0;
            foreach (var item in workItem.linkItems)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.ImageIndex = i++;
                lvi.Text = item.name;
                lvi.SubItems.Add(item.link);
                listView1.Items.Add(lvi);
            }
            listView1.Update();
        }

        private void OnDoubleSelectLinkItem(object sender, EventArgs e)
        {
            var item0 = listView1.SelectedItems[0];
            var index = workItem.linkItems.FindIndex(x=>x.name == item0.Text);
            if (index < workItem.linkItems.Count && index >= 0)
            {
                LinkItem item = workItem.linkItems[index];
                key_link_temp.Text = item.name;
                value_link_temp.Text = item.link;
                try
                {
                    System.Threading.ThreadPool.QueueUserWorkItem((x) =>
                    {
                        agent.RunCmd(item.link);
                    });
                }
                catch (Exception e0)
                {
                    MessageBox.Show("命令执行失败:" + e0.Message);
                }
            }
        }
        private void OnSelectLinkItem(object sender, EventArgs e)
        {
            var item0 = listView1.SelectedItems[0];
            var index = workItem.linkItems.FindIndex(x => x.name== item0.Text);
            if (index < workItem.linkItems.Count && index >= 0)
            {
                LinkItem item = workItem.linkItems[index];
                key_link_temp.Text = item.name;
                value_link_temp.Text = item.link;
            }
        }

        private void add_link_Click(object sender, EventArgs e)
        {
            changed = true;

            var key = key_link_temp.Text;
            var link = value_link_temp.Text;
            if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(link))
            {
                MessageBox.Show("标题或连接不能为空！");
                return;
            }
            LinkItem item = workItem.linkItems.Find(x => x.name == key);
            if (item == null)
            {
                item = new LinkItem();
                workItem.linkItems.Add(item);
            }
            item.name = key;
            item.link = link;
            ReLoadWorkItemInfo();
        }

        private void delete_link_Click(object sender, EventArgs e)
        {
            changed = true;

            var key = key_link_temp.Text;
            LinkItem item = workItem.linkItems.Find(x => x.name == key);
            if (item != null)
            {
                workItem.linkItems.Remove(item);
                ReLoadWorkItemInfo();
            }
        }
        private void import_click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(workItem.import_tablePath))
            {
                workItem.import_tablePath = "C://";
            }
            var result = Util.OpenFolderDialog("选择文件导入目录",workItem.import_tablePath);
            if (!string.IsNullOrEmpty(result))
            {
                var path = workItem.import_tablePath = result;
                if (System.IO.Directory.Exists(path))
                {
                    changed = true;
                    Util.CopyFilesTo(path, Util.GetConfigTableFolder(workItem.fileID));
                }
            }
        }
        private void export_table_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(workItem.export_tablePath))
            {
                workItem.export_tablePath = "C://";
            }
            var result = Util.OpenFolderDialog("选择文件导出目录", workItem.export_tablePath);
            if (!string.IsNullOrEmpty(result))
            {
                var path = workItem.export_tablePath = result;
                if (System.IO.Directory.Exists(path))
                {
                    Util.CopyFilesTo(Util.GetConfigTableFolder(workItem.fileID), path);
                }
            }
        }

        private void open_resource_Click(object sender, EventArgs e)
        {
            var folder = Util.GetConfigResourceFolder(workItem.fileID);
            if (System.IO.Directory.Exists(folder))
            {
                var process = Util.OpenFolder(folder);
                opendFolder.Add(process);
            }
        }

        private void open_docs_Click(object sender, EventArgs e)
        {
            var folder = Util.GetConfigDocFolder(workItem.fileID);
            if (System.IO.Directory.Exists(folder))
            {
                var process = Util.OpenFolder(folder);
                opendFolder.Add(process);
            }
        }

        private void open_config_Click(object sender, EventArgs e)
        {
            var folder = Util.GetConfigTableFolder(workItem.fileID);
            if (System.IO.Directory.Exists(folder))
            {
                var process =Util.OpenFolder(folder);
                opendFolder.Add(process);
            }
        }

        private void close_opend_folders()
        {
            foreach (var item in opendFolder)
            {
                if(item != null)
                {
                    item.Kill();
                }
            }
            opendFolder.Clear();
        }

        private void title_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(workItem.web);
            }
            catch (Exception e0)
            {
                MessageBox.Show("链接打开失败:" + e0.Message);
            }
        }
    }
}
